package cn.wolfcode.crm.service.Impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class PermissionSreviceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ApplicationContext ctx;

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> list() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(permissionMapper.selectForList(qo));
    }

    @Override
    public void reload() {
        List<String> expressions = permissionMapper.selectAllExpression();
//        从容器对象中获取所有贴Controller注解的bean
        Map<String, Object> beanMap = ctx.getBeansWithAnnotation(Controller.class);
        System.out.println(beanMap);

        Collection<Object> collectionBeans = beanMap.values();
        System.out.println(collectionBeans);

        for (Object collectionBean : collectionBeans) {
            Class<?> collectionBeanClass = collectionBean.getClass().getSuperclass();
            Method[] declaredMethods = collectionBeanClass.getDeclaredMethods();
            System.out.println(declaredMethods);
            for (Method method : declaredMethods) {
                if (method.isAnnotationPresent(RequiresPermissions.class)) {
                    RequiresPermissions requiresPermissionsAnno = method.getAnnotation(RequiresPermissions.class);
                    String[] nameAndExpression = requiresPermissionsAnno.value();
                    String expression = nameAndExpression[0];
                    String name = nameAndExpression[1];
                    if (!expressions.contains(expression)) {
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> queryByEmployeeId(Long id) {
        return permissionMapper.selectByEmployeeId(id);
    }

    @Override
    public List<String> queryExpressionsByEmployeeId(Long id) {
        return permissionMapper.selectExpressionsByEmployeeId(id);
    }

}
